// console.log( '构建开发环境的包' )
// 要求：快速构建，快速响应
let {task,src,dest,watch,series,parallel} = require('gulp')
let load = require('gulp-load-plugins')()
let del = require('del')

// 删除dist目录
task('del',async ()=>{
    del('./dist')
})
//拷贝data
task('data',async ()=>{
    src('./data/*.json')
    .pipe(dest('./dist/data'))
    .pipe(load.connect.reload())
})
// 拷贝图片
task('imgs',async ()=>{
    src('./imgs/*.*')
    .pipe(dest('./dist/imgs'))
    .pipe(load.connect.reload())
})

// 拷贝html
task('html',async ()=>{
    src('./view/*.html')
    .pipe(dest('./dist'))
    .pipe(load.connect.reload())
})

// 拷贝font
task('font',async ()=>{
    src('./font/*.*')
    .pipe(dest('./dist/font'))
    .pipe(load.connect.reload())
})

// 拷贝lib
task('lib',async ()=>{
    src('./script/lib/*.*')
    .pipe(dest('./dist/js/lib'))
    .pipe(load.connect.reload())
})

// 编译sass
task('sass',async ()=>{
    src('./sass/*.scss')
    .pipe(load.sass())
    .pipe(dest('./dist/css'))
    .pipe(load.connect.reload())
})

// 转成ES5
task('toES5',async ()=>{
    src('./script/*.js')
    .pipe(load.babel({presets: ['@babel/env']}))
    .pipe(dest('./dist/js'))
    .pipe(load.connect.reload())
})

// 启动web服务
task('webserver',async ()=>{
    load.connect.server({
        root: './dist',
        livereload: true,
        port: 3001
    })
})

// 监听文件变化
task('watchs',async ()=>{
    watch('./imgs/*.*',series('imgs','html','sass'))
    watch('./view/*.html',series('html'))
    watch('./sass/*.scss',series('sass'))
    watch('./script/*.js',series('toES5'))
    watch('./script/lib/*.*',series('lib'))
})

// 打包
task('build',series('del','data','imgs','html','font','lib','sass','toES5'))

// 组合任务
task('dev',series('build','webserver','watchs'))
